package com.example.rentingapp.view;

import android.app.Activity;

import android.graphics.drawable.BitmapDrawable;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.example.rentingapp.R;
import com.example.rentingapp.adapter.ParentCategoryAdapter;
import com.example.rentingapp.adapter.ChildrenCategoryAdapter;

public class DoubleFilterPop extends PopupWindow {
    private LayoutInflater mInflater;                                   //布局填充器
    private View popupView;                                             //pop整体View
    private Activity mActivity;                                         //上下文

    private String[] parentStrings;                                     //父类String类型的数据
    private String[][] childrenStrings;                                 //子类String类型的数据

    private ListView lvParentCategory = null;                           //选择父类条件的list
    private ListView lvChildrenCategory = null;                         //选择子类条件的list
    private Button btn_double_pop;                                      //按钮

    private ParentCategoryAdapter parentCategoryAdapter = null;         //父类适配器
    private ChildrenCategoryAdapter childrenCategoryAdapter = null;     //子类适配器
    private SelectCategory selectCategory = null;                       //监听器

    private DisplayMetrics dm;                                          //获取手机屏幕分辨率的类

    //构造函数
    public DoubleFilterPop(Activity activity, String[] parentStrings, String[][] childrenStrings, SelectCategory selectCategory) {
        this.selectCategory = selectCategory;
        this.parentStrings = parentStrings;
        this.childrenStrings = childrenStrings;
        this.mActivity = activity;
        this.mInflater = LayoutInflater.from(mActivity);
        popupView = mInflater.inflate(R.layout.double_popup_list_dialog, null);             //布局填充
        this.setContentView(popupView);                                                     //设置View
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);                                 //设置弹出窗体的宽
        dm = new DisplayMetrics();                                                          //获取屏幕高度
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        this.setHeight((dm.heightPixels) * 1 / 3);                                          //设置弹出窗体的高
        initPopView();                                                                      //初始化控件
        this.setFocusable(true);                                                            //设置焦点
        this.setTouchable(true);                                                            //内部点击事件
        this.setOutsideTouchable(true);                                                     //外部点击事件
        this.setBackgroundDrawable(new BitmapDrawable());                                   //设置背景
        this.setAnimationStyle(R.style.pop_anim);                                           //设置加载动画
        this.update();
    }

    public void initPopView() {
        //父类别适配器
        lvParentCategory = (ListView) popupView.findViewById(R.id.lv_item1);
        parentCategoryAdapter = new ParentCategoryAdapter(mActivity, parentStrings);
        lvParentCategory.setAdapter(parentCategoryAdapter);

        //子类别适配器
        lvChildrenCategory = (ListView) popupView.findViewById(R.id.lv_item2);
        childrenCategoryAdapter = new ChildrenCategoryAdapter(mActivity);
        lvChildrenCategory.setAdapter(childrenCategoryAdapter);

        //按钮适配器
        btn_double_pop = (Button) popupView.findViewById(R.id.btn_double_pop);
        //显示全部的子类型作为初始背景
        childrenCategoryAdapter.setDatas(childrenStrings[0]);

        //点击事件监听器
        lvParentCategory.setOnItemClickListener(parentItemClickListener);
        lvChildrenCategory.setOnItemClickListener(childrenItemClickListener);
        btn_double_pop.setOnClickListener(btnDoubleClickListener);
    }

    //父类别点击事件
    private OnItemClickListener parentItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            childrenCategoryAdapter.setDatas(childrenStrings[position]);
            childrenCategoryAdapter.notifyDataSetChanged();

            parentCategoryAdapter.setSelectedPosition(position);
            parentCategoryAdapter.notifyDataSetChanged();
        }
    };

    //子类别点击事件
    private OnItemClickListener childrenItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            if (selectCategory != null) {
                selectCategory.selectCategory(parentCategoryAdapter.getPos(), position);
            }
            dismiss();
        }
    };

    //Button点击事件
    private View.OnClickListener btnDoubleClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    };

    //选择成功回调
    public interface SelectCategory {
        //parentSelectPosition   父类别选中下标
        //childrenSelectPosition 子类别选中下标
        void selectCategory(int parentSelectPosition, int childrenSelectPosition);
    }
}
